import { Graph } from '@antv/x6';

export function registerConnector(graph) {
  // 注册平滑连接器
  Graph.registerConnector(
    'smooth',
    (sourcePoint, targetPoint, routePoints, options) => {
      const points = [sourcePoint, ...routePoints, targetPoint];
      const curves = [];
      
      for (let i = 0; i < points.length - 1; i++) {
        const current = points[i];
        const next = points[i + 1];
        
        if (i === 0) {
          curves.push(`M ${current.x} ${current.y}`);
        }
        
        const midX = (current.x + next.x) / 2;
        const midY = (current.y + next.y) / 2;
        
        curves.push(`Q ${midX} ${current.y} ${midX} ${midY}`);
        curves.push(`Q ${midX} ${next.y} ${next.x} ${next.y}`);
      }
      
      return curves.join(' ');
    },
    true
  );

  // 注册直线连接器
  Graph.registerConnector(
    'line',
    (sourcePoint, targetPoint, routePoints, options) => {
      const points = [sourcePoint, ...routePoints, targetPoint];
      const curves = [];
      
      for (let i = 0; i < points.length; i++) {
        const point = points[i];
        if (i === 0) {
          curves.push(`M ${point.x} ${point.y}`);
        } else {
          curves.push(`L ${point.x} ${point.y}`);
        }
      }
      
      return curves.join(' ');
    },
    true
  );

  // 注册直角连接器
  Graph.registerConnector(
    'rounded',
    (sourcePoint, targetPoint, routePoints, options) => {
      const points = [sourcePoint, ...routePoints, targetPoint];
      const curves = [];
      
      for (let i = 0; i < points.length - 1; i++) {
        const current = points[i];
        const next = points[i + 1];
        
        if (i === 0) {
          curves.push(`M ${current.x} ${current.y}`);
        }
        
        // 创建直角连接
        if (Math.abs(current.x - next.x) > Math.abs(current.y - next.y)) {
          // 水平优先
          curves.push(`L ${next.x} ${current.y}`);
          curves.push(`L ${next.x} ${next.y}`);
        } else {
          // 垂直优先
          curves.push(`L ${current.x} ${next.y}`);
          curves.push(`L ${next.x} ${next.y}`);
        }
      }
      
      return curves.join(' ');
    },
    true
  );
} 